﻿using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.Scalesetting
{
    /// <summary>
    /// 活畜称重心事列表、查询
    /// </summary>
    public class LiveAnimaWeightSelectListHander : IRequestHandler<LiveAnimalWeightSelectListCommand, Resqults<LiveAnimalWeightSelectListDTO>>
    {
        private readonly IBateRepository<LiveAnimalweighing> live;
        private readonly IBateRepository<Approach> appro;

        public LiveAnimaWeightSelectListHander(IBateRepository<LiveAnimalweighing> live, IBateRepository<Approach> appro)
        {
            this.live = live;
            this.appro = appro;
        }

        public async Task<Resqults<LiveAnimalWeightSelectListDTO>> Handle(LiveAnimalWeightSelectListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<LiveAnimalWeightSelectListDTO> res = new Resqults<LiveAnimalWeightSelectListDTO>();
                var list = from a in live.GetAll()
                           join b in appro.GetAll() on a.ApproachNo equals b.ApproachNo
                           where a.State == 0
                           select new LiveAnimalWeightSelectListDTO()
                           {
                               LiveAnimalweighingId = a.LiveAnimalweighingId,
                               WeighTime=a.WeighTime,
                               LiveAnimalweighingNo=a.LiveAnimalweighingNo,
                               ApproachNo=a.ApproachNo,
                               Suppliers=b.Suppliers,
                               licensePlate=b.licensePlate,
                               ApproachBatch=b.ApproachBatch,
                               WeightNum=a.WeightNum,
                               NetWeight=a.NetWeight,
                               AverageWeight= a.AverageWeight   
                           };
                if (request.WeighTime != null)
                {
                    list = list.Where(x => x.WeighTime == request.WeighTime);
                }
                if (!string.IsNullOrEmpty(request.Suppliers))
                {
                    list = list.Where(x => x.Suppliers .Contains(request.Suppliers));
                }
                if (!string.IsNullOrEmpty(request.ApproachBatch))
                {
                    list = list.Where(x => x.ApproachBatch .Contains(request.ApproachBatch));
                }
                res.Date = list.ToList();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
